-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
PK tests: use PSA to generate keypairs when USE_PSA is enabled #7393
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One problem with error management, and similar points about scope as previous PRs.
37a27db
to
8ffeddc
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Rebased after most recent updates in #7392 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Instead of using the legacy mbedtls_ecp_gen_keypair() which makes use of ECP's math, when USE_PSA_CRYPTO is enabled then the new function pk_genkey_ec() is used in test_suite_pk. Signed-off-by: Valerio Setti <valerio.setti@nordicsemi.no>
Signed-off-by: Valerio Setti <valerio.setti@nordicsemi.no>
Signed-off-by: Valerio Setti <valerio.setti@nordicsemi.no>
Signed-off-by: Valerio Setti <valerio.setti@nordicsemi.no>
Signed-off-by: Valerio Setti <valerio.setti@nordicsemi.no>
Signed-off-by: Valerio Setti <valerio.setti@nordicsemi.no>
Rebased after #7392 has been merged into |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
|
||
status = psa_generate_key(&key_attr, &key_id); | ||
if (status != PSA_SUCCESS) { | ||
return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is only test but I think we should also destroy key if key generation fails in the middle.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it seems to me that this is the only point in that function for which there is a return
instead of goto exit
. However this seems ok to me since if psa_generate_key
fails then there should be nothing to destroy. Other failures destroy the key. What am I missing?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that is all true. I'm not sure if we shouldn't also destroy key here. But looking at the description of psa_generate_key
:
On success, an identifier for the newly created key.
It seems that current version is correct.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, I would expect psa_generate_key()
to act in an atomic way: either it succeeds, or it leave the key slot unchanged, so that callers don't need to call destroy on failure. However I checked the documentation and it doesn't say explicitly. I checked other uses in the library, and they seem to only call destroy when generate succeeded but an error occurred later. So I think the code is fine as it is, but out of precaution I'll ask on the PSA Crypto API channel.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
However I checked the documentation and it doesn't say explicitly.
Well, I apparently didn't get enough sleep last night, because the documentation does say: "key On success, an identifier for the newly created key. PSA_KEY_ID_NULL on failure." So, we don't need to call destroy()
on failure here. (It would however be legal, as psa_destroy_key(PSA_KEY_ID_NULL)
is guaranteed to do nothing an return success.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a general principle that a PSA function doesn't affect the system state on failure. (It might leave its in-out arguments in a changed state on failure, however: multipart operation functions put the operation object in an error state when they fail.)
psa_generate_key
and the other key creation functions are guaranteed to set the key_id
output argument to 0 on failure. So it's guaranteed that calling psa_destroy_key(key_id)
is always correct, but it's also guaranteed that it's a no-op if the creation function fails.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, I should refresh the page more often when replying to comments :) I hadn't seen your replies when I wrote mines.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
pk: add alternate function for keypair generation using PSA
Instead of using the legacy
mbedtls_ecp_gen_keypair()
which makes use of ECP's math, whenUSE_PSA_CRYPTO
is enabled then the new functionpk_genkey_ec()
is used intest_suite_pk
.Depends on #7392Resolves #7389
Gatekeeper checklist